/**************************************************************
* FileName:      mail_file_opt.c
* Author:        liu.sh
* Version :      0.1
* Date:          2008-09-24
* Description:   �ļ������ӿڡ�
* Function List:
* 1----	int mail_open_file(char *pfile_name);
* 2----	int mail_append_file(int file_handle, char *data, int data_len);
* 3----	int mail_get_file_size(int file_handle);
* 4----	int mail_read_file(int file_handle, int index, int read_size, char *buff);
* 5----	int mail_close_file(int file_handle);
* History:
*  <author>    <time>   <version >   <desc>
*   liu.sh    08-09-24     0.1       create
***************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

/**************************************************************************
 *	Function:	mail_open_file
 *	Description:���ļ��������Զ���������д��ʽ��
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:pfile_name	�ļ����ַ���·����ָ��
 *	Output:
 *	Return:				void* �ɹ������ļ�ָ�룬ʧ�ܷ���NULL
 *	Others:
 *************************************************************************/
void* mail_open_file(char *pfile_name)
{
	FILE *fp;

	if(NULL == pfile_name)
	{
		fprintf(stderr, "Parameter pointer is NULL!\n");
		return NULL;
	}

	if((fp=fopen(pfile_name,"ab+"))==NULL)
	{
		fprintf(stderr, "cannot open [%s] file![%d]\n", pfile_name, ferror(fp));
		return NULL;
	}

	return((void *)fp);
}

/**************************************************************************
 *	Function:	mail_dele_file
 *	Description:�ҵ�ָ���ļ�������Ӵ���ɾ��
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:pfile_name	�ļ����ַ���·����ָ��
 *	Output:
 *	Return:			int �ɹ�����0��ʧ�ܣ�1
 *	Others:
 *************************************************************************/
int mail_dele_file(char *pfile_name)
{
	FILE *fp;

	if(NULL == pfile_name)
	{
		fprintf(stderr, "Parameter pointer is NULL!\n");
		return -1;
	}

	if(remove(pfile_name) < 0)
	{
		fprintf(stderr, "cannot open [%s] file![%d]\n", pfile_name, ferror(fp));
		return -1;
	}

	return(0);
}

/**************************************************************************
 *	Function:	mail_append_file
 *	Description:���ļ�ĩβ׷�����
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:fp			�ļ�ָ��
 *	Input:data			���ָ��
 *	Input:data_len		��ݳ���
 *	Output:
 *	Return:			int ʵ��д����ݵĴ�С,ʧ�ܷ��أ�1
 *	Others:
 *************************************************************************/
int mail_append_file(void *fp, char *data, int data_len)
{
	int ret = 0;
	if(NULL == fp)
	{
		fprintf(stderr,  "Parameter pointer is NULL!\n");
		return 0;
	}

	if(fseek(fp, 0, SEEK_END))
	{
		fprintf(stderr,  "Move file pointer to end fail![%d]\n", ferror(fp));
		return -1;
	}

	ret = fwrite(data, 1, data_len, fp);
	if(ret < data_len)
	{
		fprintf(stderr,  "write file pointer fail![%d]\n", ferror(fp));
	}
	return ret;
}

/**************************************************************************
 *	Function:	mail_get_file_size
 *	Description:��ȡ�ļ���С
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:fp		�ļ�ָ��
 *	Output:
 *	Return:			long �ɹ������ļ���С��ʧ�ܷ��أ�1
 *	Others:
 *************************************************************************/
long mail_get_file_size(void *fp)
{
	long ret = 0;
	if(NULL == fp)
	{
		fprintf(stderr,  "Parameter pointer is NULL!\n");
		return 0;
	}

	if(fseek(fp, 0, SEEK_END))
	{
		fprintf(stderr,  "Move file pointer to end fail![%d]\n", ferror(fp));
		return -1;
	}

	ret = ftell(fp);

	return ret;
}

/**************************************************************************
 *	Function:	mail_read_file
 *	Description:��ȡָ����С����ݴ��ļ�
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:fp			�ļ�ָ��
 *	Input:index			��ȡƫ�ƣ����ļ���ʼ��ƫ�ƣ�
 *	Input:read_size		buff��С
 *	Input:buff			��ȡ���ָ��
 *	Output:
 *	Return:			int �ɹ�����ʵ�ʶ�ȡ��С��ʧ�ܷ��� < 0
 *	Others:
 *************************************************************************/
int mail_read_file(void *fp, int index, int read_size, char *buff)
{
	int ret = 0;
	if(NULL == fp)
	{
		fprintf(stderr,  "Parameter pointer is NULL!\n");
		return 0;
	}

	if(fseek(fp, index, SEEK_SET))
	{
		fprintf(stderr,  "Move file pointer to end fail [%d]!\n", ferror(fp));
		return -1;
	}

	ret = fread(buff, sizeof(char), read_size, fp);

	if(ret < read_size && !feof(fp))
	{
		fprintf(stderr,  "Read file error [%d]!\n", ferror(fp));
		return -2;
	}

	return ret;
}

/**************************************************************************
 *	Function:	mail_close_file
 *	Description:�ر��ļ�
 *	Calls:
 *	Called By:
 *	Table Accessed:
 *	Table Updated:
 *	Input:fp		�ļ�ָ��
 *	Output:
 *	Return:			int 0
 *	Others:
 *************************************************************************/
int mail_close_file(void *fp)
{
	if(NULL == fp)
	{
		fprintf(stderr,  "Parameter pointer is NULL!\n");
		return 0;
	}
	fclose(fp);
	return 0;
}

///////////////////////////////////////////////////////////////////////////
void link_path_name(char *filename, char *my_path, char *result)
{
	strcpy(result, my_path);
	if (('/' == *(my_path + strlen(my_path) - 1)) && ('/' == *filename)) {
		strcat(result, filename + 1);
	} else if (('/' != *(my_path + strlen(my_path) - 1)) && ('/' != *filename)) {
		strcat(result, "/");
		strcat(result, filename);
	} else {
		strcat(result, filename);
	}
}



/**************************************************************************
 *	Function:		adpt_sys_read
 *	Description:	 ?����????t
 *	Input:		filename, 		???t??
 *				fp, 				???t?����?��?
 *				read_size			?����?3��?��
 *	Output:		buff 				?����?????
 *				feof_flg			��?��?������?��????t?2
 *	Return:		��??o��y     		3��1|?����?��??����y
 *				????				����㨹
 *	Others:		
 *************************************************************************/
inline int adpt_sys_read(int fd, int offset, char *buff, int read_size)
{
	int ret = 0;
	lseek(fd, offset, SEEK_SET);
	ret = read(fd, buff, read_size);
	if(ret < 0)
	{
		fprintf(stderr, "%d:%s\n", errno, strerror(errno));
	}
	return ret;
}


/**************************************************************************
 *	Function:		adpt_sys_getsize
 *	Description:	 ??��????t�䨮D?
 *	Input:		filename, 			???t??
 *	Output:
 *	Return:		?y?��	 		3��?��?��
 *				0				����㨹	
 *	Others:		
 *************************************************************************/
inline int adpt_sys_getsize(int fd)
{
	return lseek(fd, 0, SEEK_END);
}


/**************************************************************************
 *	Function:		adpt_sys_delete
 *	Description:	 ��?3y???t��?������?����?��
 *	Input:		filename, 			???t??
 *	Output:
 *	Return:		��????�䨢??����?key????
 *	Others:		
 *************************************************************************/
inline int adpt_sys_delete(char *filename, char *filepath)
{	
	char full_name[256] = {};
	link_path_name(filename, filepath, full_name);
	return remove(full_name);
}


/**************************************************************************
 *	Function:		adpt_sys_write
 *	Description:	D��???t?��?��
 *	Input:		filename, 			???t??
 *				fp, 					???t?����?��?
 *				buf					D�䨨??����Y
 *				buf_len 				D�䨨??����Y3��?��
 *	Output:		write_len 			����?��D�䨨?3��?��
 *	Return:		DDM_NO_ERROR 		3��1|
 *				DDM_OVER_MAX		3?1y�����䨮D?
 *				DDM_LACK_SPACE 	????2?1?
 *				????				����㨹	
 *	Others:		
 *************************************************************************/
inline int adpt_sys_write(int fd, char *buf, int buf_len)
{
	int ret = 0;
#ifdef _DEBUG_____

	char *p_tmp = calloc(1, buf_len+1);
	if(NULL == p_tmp)
	{
		return -9;
	}
	memcpy(p_tmp, buf, buf_len);
	ret = write(fd, p_tmp, buf_len);
	free(p_tmp);
#else
	ret = write(fd, buf, buf_len);
	if(ret < buf_len)
	{
		fprintf(stderr, "%d:%s\n", errno, strerror(errno));
	}
	
#endif
	return ret;
}


/**************************************************************************
 *	Function:		adpt_sys_open
 *	Description:	 ??3y��?2????t��?������?����?��
 *	Input:		filename, 			???t??
 *				my_path, 			???t?��??
 *				set_max				???t����3��?��
 *				ptcp					ptcp????
 *				link_direction			tcp��y?Y����?�䡤??��
 *				tcp_type				������?��?������?����??
 *	Output:
 *	Return:		???t?����?��? 	3��1|
 *				NULL				����㨹
 *	Others:		
 *************************************************************************/
inline int adpt_sys_open(char *filename, char *filepath)
{
	char full_name[256] = {};
	link_path_name(filename, filepath, full_name);
	return open(full_name, O_RDWR | O_CREAT | O_APPEND, S_IRWXU);
}


/**************************************************************************
 *	Function:		adpt_sys_close
 *	Description:	 1?��????t
 *	Input:			
 *	Output:
 *	Return:		DDM_NO_ERROR 	3��1|
 *				????			����㨹
 *	Others:		
 *************************************************************************/
inline int adpt_sys_close(int fd)
{
	return close(fd);
}

